/*
                  InterfaceGameSummary Class Implementation
*/
#include "InterfaceGameSummary.h"

InterfaceGameSummary::InterfaceGameSummary()
{

}

InterfaceGameSummary::~InterfaceGameSummary()
{
    PlayerTabs.clear();
    PlayerSummaryFrames.clear();
}

int InterfaceGameSummary::Initialise()
{
    frmOptionbar = new Frame;
    frmOptionbar->Initialise(Point(0, 0), ConfigFile::iCurrentWindowResX, 50);
    frmOptionbar->setVisible(true);
    frmOptionbar->setFixed(true);
    frmOptionbar->WindowAlign(ALIGN_LEFT, GetWindowSize());
    frmOptionbar->WindowAlign(ALIGN_BOTTOM, GetWindowSize());

    btnBack = new Button;
    btnBack->Initialise(Point(50, 0), "Back to Menu", "data//fonts//gui.ttf", 18, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(0, 0, 0));

    frmOptionbar->AddWidget(btnBack, "btnBack");

    frmTabs = new Frame;
    frmTabs->Initialise(Point(50, 100), 1, 1);
    frmTabs->setBackground("data//interface//options//optiontabsbg.png");
    frmTabs->setVisible(true);
    frmTabs->setFixed(true);

    btnOverview = new Button;
    btnOverview->Initialise(Point(50, 50), "Overview", "data//fonts//gui.ttf", 18, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(0, 0, 0));

    frmOverview = new Frame;
    frmOverview->Initialise(Point(0, 0), 1, 1);
    frmOverview->setBackground("data//interface//options//optionframe.png");
    frmOverview->setVisible(true);
    frmOverview->setFixed(true);
    frmOverview->WindowAlign(ALIGN_RIGHT, GetWindowSize());
    frmOverview->MoveRelative(-50, 100);

    lblWinner = new Label;
    lblWinner->Initialise(Point(50, 25), "Data//Fonts//gui.ttf", 24, "Winner: ", GetSDLColor(255, 255, 255));

    lblWinnerName = new Label;
    lblWinnerName->Initialise(Point(200, 25), "Data//Fonts//gui.ttf", 24, "", GetSDLColor(255, 255, 255));

    if (SummaryInfo::iWinner != -1)
    {
        lblWinnerName->setText(SummaryInfo::PlayerInfo.at(SummaryInfo::iWinner)->strName);
    }

    frmOverview->AddWidget(lblWinner, "lblWinner");
    frmOverview->AddWidget(lblWinnerName, "lblWinnerName");

    for (int i = 0; i < SummaryInfo::PlayerInfo.size(); i++)
    {
        PlayerTabs.push_back(new Button);

        PlayerTabs.back()->Initialise(Point(50, 100 + (i * 50)), SummaryInfo::PlayerInfo.at(i)->strName, "data//fonts//gui.ttf", 18, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(0, 0, 0));

        frmTabs->AddWidget(PlayerTabs.back(), "btn" + SummaryInfo::PlayerInfo.at(i)->strName + "Tab");

        PlayerSummaryFrames.push_back(new PlayerSummaryFrame);

        PlayerSummaryFrames.back()->Initialise(Point(0, 0), 1, 1);
        PlayerSummaryFrames.back()->setBackground("data//interface//options//optionframe.png");
        PlayerSummaryFrames.back()->setVisible(false);
        PlayerSummaryFrames.back()->setFixed(true);
        PlayerSummaryFrames.back()->WindowAlign(ALIGN_RIGHT, GetWindowSize());
        PlayerSummaryFrames.back()->MoveRelative(-50, 100);

        PlayerSummaryFrames.back()->lblName->setText(SummaryInfo::PlayerInfo.at(i)->strName);
        PlayerSummaryFrames.back()->lblNumScore->setText(lexical_cast<string>(SummaryInfo::PlayerInfo.at(i)->iScore));
        PlayerSummaryFrames.back()->lblNumTotalScore->setText(lexical_cast<string>(SummaryInfo::PlayerInfo.at(i)->iTotalGameScore));
        PlayerSummaryFrames.back()->lblNumKills->setText(lexical_cast<string>(SummaryInfo::PlayerInfo.at(i)->iKills));
        PlayerSummaryFrames.back()->lblNumDeaths->setText(lexical_cast<string>(SummaryInfo::PlayerInfo.at(i)->iDeaths));
        PlayerSummaryFrames.back()->lblNumKDRatio->setText(lexical_cast<string>(SummaryInfo::PlayerInfo.at(i)->fKillDeathRatio));

        frmgSummary.AddFrame(PlayerSummaryFrames.back() , "frm" + SummaryInfo::PlayerInfo.at(i)->strName + "Frame");
    }

    frmTabs->AddWidget(btnOverview, "btnOverview");

    frmgSummary.AddFrame(frmOverview, "frmOverview");
    frmgSummary.AddFrame(frmTabs, "frmTabs");
    frmgSummary.AddFrame(frmOptionbar, "frmOptionbar");

    SummaryInfo::FreeInfo();
}

InterfaceType InterfaceGameSummary::HandleInput(const SDL_Event& event)
{
    frmgSummary.parseInput(event);

    return CheckWidgetEvents();
}

int InterfaceGameSummary::Draw(SDL_Surface *surface)
{
    frmgSummary.BlitToSurface(surface);
}

InterfaceType InterfaceGameSummary::CheckWidgetEvents()
{
    GUIEvent guievent;

    while (frmOptionbar->PollEvent(guievent) == 0)
    {
        switch (guievent.getEventType())
        {
            case GUIEVENT_LEFTMOUSEUP:
            {
                if (guievent.getSource() == "btnBack")
                {
                    return MENU;
                }
            }
        }
    }

    while (frmTabs->PollEvent(guievent) == 0)
    {
        switch (guievent.getEventType())
        {
            case GUIEVENT_LEFTMOUSEUP:
            {
                if (guievent.getSource() == "btnOverview")
                {
                    frmOverview->setVisible(true);

                    HideSummaryFrames();
                }

                for (int i = 0; i < PlayerTabs.size(); i++)
                {
                    if (guievent.getSource() == PlayerTabs.at(i)->getName())
                    {
                        HideSummaryFrames();

                        frmOverview->setVisible(false);

                        PlayerSummaryFrames.at(i)->setVisible(true);
                    }
                }
            }
        }
    }

    return NONE;
}

void InterfaceGameSummary::HideSummaryFrames()
{
    for (int i = 0; i < PlayerSummaryFrames.size(); i++)
    {
        PlayerSummaryFrames.at(i)->setVisible(false);
    }
}
